package cn.legend.travel.admin.user.dao.persist.repository.impl;

import cn.legend.travel.admin.user.dao.persist.mapper.UserMapper;
import cn.legend.travel.admin.user.dao.persist.repository.IUserRepository;
import cn.legend.travel.admin.user.pojo.entity.User;
import cn.legend.travel.admin.user.pojo.vo.UserCheckVO;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements IUserRepository {
    private final UserMapper mapper;

    @Override
    public PageData<UserCheckVO> getAllUsers(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.getAllUsers()));
    }

    @Override
    public PageData<UserCheckVO> getUsersForCheck(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.getUsersForCheck()));
    }

    @Override
    public int update(User user) {
        return mapper.updateById(user);
    }

    @Override
    public User getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void unfreeze(Long id) {
        User user = new User();
        user.setId(id);
        user.setState(1);
        mapper.updateById(user);
    }

    @Override
    public UserCheckVO getByUsername(String username) {
        return mapper.getByUsername(username);
    }

    @Override
    public UserCheckVO getByPhone(String phone) {
        return mapper.getByPhone(phone);
    }
}
